﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GridMaker : MonoBehaviour 
{	
	public int width = 9;
	public int height = 9;
	public float gemWidth = 30;
	public float gemHeight = 30;
	public GameObject[] possibleGems;
	public GameObject[] otherGems;
	
	[HideInInspector]
	public Gem[,] gems;
	//Dictionnaire pour acceder a mes "Gems(index)" pour les vérifications.
	public Dictionary<GameObject,Gem> indexedGems;
	
	private int numberOfTurns = 0;
	private bool playerTurn = false;
	
	 
	// Build the grid
	private void Start () 
	{	
		indexedGems = new Dictionary<GameObject, Gem>();
		gems = new Gem[9,9]; 
		//scan the grid ton put gems in it (x). 
		for (int i = 0; i < width; i++)
		{
			//scan the grid to put gems in it (y).
			for(int j = 0; j < height; j++)
			{
				int randomGem = Random.Range(0, 6);
				int otherGems = GetOtherGem(randomGem);
				
				Gem currentGem = new Gem();
				//gems[i,j] = currentGem;
				currentGem.go = Instantiate(possibleGems[randomGem], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
				currentGem.i = i;
				currentGem.j = j;
				gems[i,j] = currentGem;
				
				//first verification to randomize initial grid.
				if (i > 1)
				{	
					if(gems[i-1,j].go.name == gems[i,j].go.name && gems[i-2,j].go.name == gems[i,j].go.name)
					{	
						Destroy(gems[i,j].go);
						currentGem.go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
					}
				}
				if (j > 1)
				{	
					if(gems[i,j-1].go.name == gems[i,j].go.name && gems[i,j-2].go.name == gems[i,j].go.name)
					{
						Destroy(gems[i,j].go);
						currentGem.go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
					}
				}
				indexedGems.Add(currentGem.go,currentGem);
				//Debug.Log(gems[i,j].go.name);
				//Debug.Log (currentGem.go.name);			
			}
		}
	}	
	
	public int GetOtherGem(int number)
	{	
		bool isSameNumber = true;
		int randomGem = 0;
	
		while(isSameNumber)
		{
			randomGem = Random.Range(0, 6);
			if(randomGem != number)
			{
				isSameNumber = false;
			}
			
		}
		return randomGem;
	}
	public bool CheckMatchThree()
	{
		CheckLigne5();
		CheckColonne5 ();
		CheckLigne4 ();
		CheckColonne4 ();
		CheckLigne3 ();
		CheckColonne3 ();	
		return false;
	}
	
	private void Update()
	{
		//CheckMatchThree();
	}
	
	private bool CheckLigne3 ()
	{	
		
		bool jaiUneLigne = false;
		
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{	
				if (i < width-2)
				{	
					if (gems[i,j].go.name == gems[(i+1),j].go.name && gems[i,j].go.name == gems[(i+2),j].go.name)
					{
						Debug.Log("There is a ligne");
						Destroy(gems[i,j].go);
						Destroy(gems[i+1,j].go);
						Destroy(gems[i+2,j].go);
						jaiUneLigne = true;
					}
				}	
			}
		}
		return jaiUneLigne;
	}

	private bool CheckColonne3 ()
	{
		bool jaiUneColonne = false;
		
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				if (j < height-2)
				{
					if (gems[i,j].go.name == gems[i,(j+1)].go.name && gems[i,j].go.name == gems[i,(j+2)].go.name)
					{	
						Destroy(gems[i,j].go);
						Destroy(gems[i,j+1].go);
						Destroy(gems[i,j+2].go);
						//gems[i,j].go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
						jaiUneColonne = true;
					}
				}	
			}
		}
		return jaiUneColonne;
	}
	
	private bool CheckLigne4 ()
	{
		bool jaiUneLigne4 = false;
		
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				//Debug.Log(gems[i,j].go.name);
				if (i < width-3)
				{
					if (gems[i,j].go.name == gems[(i+1),j].go.name && gems[i,j].go.name == gems[(i+2),j].go.name && gems[i,j].go.name == gems[(i+3),j].go.name)
					{	
						Destroy(gems[i,j].go);
						Destroy(gems[i+1,j].go);
						Destroy(gems[i+2,j].go);
						Destroy(gems[i+3,j].go);
						//gems[i,j].go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
						jaiUneLigne4 = true;
					}
				}	
			}
		}
		return jaiUneLigne4;
	}
	
	private bool CheckColonne4 ()
	{	
		bool jaiUneColonne4 = false;
		
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				//Debug.Log(gems[i,j].go.name);
				if (j < height-3)
				{
					if (gems[i,j].go.name == gems[i,(j+1)].go.name && gems[i,j].go.name == gems[i,(j+2)].go.name && gems[i,j].go.name == gems[i,(j+3)].go.name)
					{	
						Destroy(gems[i,j].go);
						Destroy(gems[i,j+2].go);
						Destroy(gems[i,j+2].go);
						Destroy(gems[i,j+3].go);
						//gems[i,j].go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
						//Debug.Log("there is a colonne of 4");
						jaiUneColonne4 = true;
					}
				}	
			}
		}
		return jaiUneColonne4;
	}
	
	private bool CheckLigne5 ()
	{	
		bool jaiUneLigne5 = false;
		
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				//Debug.Log(gems[i,j].name);
				if (i < width-4)
				{
					if (gems[i,j].go.name == gems[(i+1),j].go.name && gems[i,j].go.name == gems[(i+2),j].go.name && gems[i,j].go.name == gems[(i+3),j].go.name && gems[i,j].go.name == gems[(i+4),j].go.name)
					{	
						Destroy(gems[i,j].go);
						Destroy(gems[i+1,j].go);
						Destroy(gems[i+2,j].go);
						Destroy(gems[i+3,j].go);
						Destroy(gems[i+4,j].go);
						//gems[i,j].go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
						jaiUneLigne5 = true; 
					}
				}	
			}
		}
		return jaiUneLigne5; 
	}
	
	private bool CheckColonne5 ()
	{
		bool jaiUneColonne5 = false;
		
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				if (j < height-4)
				{
					if (gems[i,j].go.name == gems[i,(j+1)].go.name && gems[i,j].go.name == gems[i,(j+2)].go.name && gems[i,j].go.name == gems[i,(j+3)].go.name && gems[i,j].go.name == gems[i,(j+4)].go.name)
					{	
						Destroy(gems[i,j].go);
						Destroy(gems[i,j+1].go);
						Destroy(gems[i,j+2].go);
						Destroy(gems[i,j+3].go);
						Destroy(gems[i,j+4].go);
						//gems[i,j].go = Instantiate(possibleGems[otherGems], new Vector3(i*gemWidth, j*gemHeight,0), Quaternion.identity) as GameObject;
						//Debug.Log("there is a colonne of 5");
						jaiUneColonne5 = true;
					}
				}	
			}
		}
		return jaiUneColonne5;				
	}
	
	/*private bool Victory()
	{
		
	
	}*/
}
